package com.dsg.controller.apply;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dsg.common.constants.MessageConstants;
import com.dsg.common.dto.AjaxResult;
import com.dsg.entity.apply.DsgApplyBill;
import com.dsg.entity.apply.DsgApplyCar;
import com.dsg.entity.apply.DsgApplyResult;
import com.dsg.entity.dto.CheckResultDto;
import com.dsg.entity.dto.ResultDetailDto;
import com.dsg.service.apply.*;
import com.dsg.workflow.WorkflowEngine;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import com.dsg.controller.base.BaseController;

import java.awt.geom.QuadCurve2D;

/**
 * <p>
 * 業務申請單內審結果 前端控制器
 * </p>
 *
 * @author system
 * @since 2020-08-31
 */
@RestController
@RequestMapping("/apply/dsgApplyResult")
public class DsgApplyResultController extends BaseController {
    @Autowired
    IDsgApplyResultService service;
    @Autowired
    IDsgApplyResultService resultService;
    @Autowired
    IDsgApplyCarService carService;
    @Autowired
    IDsgApplyPayService payService;
    @Autowired
    IDsgApplyCusService cusService;
    @Autowired
    IDsgApplyBillService billService;
    @Autowired
    WorkflowEngine engine;

    @GetMapping(value = "/viewAudit")
    @ApiOperation("对单转车牌、转油种显示内审记录")
    public AjaxResult viewAudit(String appNo) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("APP_NO", appNo);
        wrapper.eq("RESULT_TYPE", DsgApplyResult.RESULT_TYPE_CAR);
        DsgApplyResult result = service.getOne(wrapper);
        return json(MessageConstants.SSO_STATUS_SUCCESS, result);
    }

    @GetMapping(value = "/selectCheckResult")
    @ApiOperation("記賬咭內部審批-列表")
    @ApiResponses(
            @ApiResponse(response = CheckResultDto.class, code = 0, message = "ok")
    )
    public AjaxResult selectCheckResult(String appNo, Integer keepType) {
        Page page = getPage();
        page = service.selectCheckResult(page, appNo, getEntCode(), keepType);
        AjaxResult ajaxResult = json(MessageConstants.SSO_STATUS_SUCCESS, page.getRecords(), (int) page.getTotal());
        return ajaxResult;
    }

    @GetMapping(value = "/resultDetail")
    @ApiOperation("記賬咭內部審批-詳細列表")
    @ApiResponses(
            @ApiResponse(response = ResultDetailDto.class, code = 0, message = "ok")
    )
    public AjaxResult resultDetail(String appNo) {
        return success(service.resultDetail(appNo));
    }

    @PostMapping(value = "/cancelCar")
    @ApiOperation("記賬咭內部審批-取消車")
    @ApiResponses(
            @ApiResponse(response = ResultDetailDto.class, code = 0, message = "ok")
    )
    public AjaxResult cancelCar(String appNo, String carNo) {
        LambdaUpdateWrapper<DsgApplyCar> wrapper = new LambdaUpdateWrapper();
        wrapper.eq(DsgApplyCar::getAppNo, appNo);
        wrapper.eq(DsgApplyCar::getCarNo, carNo);
        DsgApplyCar car = new DsgApplyCar();
        car.setCarStatus(DsgApplyCar.CAR_STATUS_CANCEL);
        carService.update(car, wrapper);
        return success();
    }

    @PostMapping(value = "/forcePass")
    @ApiOperation("記賬咭內部審批-強行通過")
    @ApiResponses(
            @ApiResponse(response = ResultDetailDto.class, code = 0, message = "ok")
    )
    public AjaxResult forcePass(String appNo, String carNo) {
        LambdaUpdateWrapper<DsgApplyResult> wrapper = new LambdaUpdateWrapper();
        wrapper.eq(DsgApplyResult::getAppNo, appNo);
        wrapper.eq(DsgApplyResult::getResultName, carNo);
        wrapper.eq(DsgApplyResult::getResultType, DsgApplyResult.RESULT_TYPE_CAR);
        DsgApplyResult result = new DsgApplyResult();
        result.setResultStatus(DsgApplyResult.RESULT_STATUS_FORCE);
        resultService.update(result, wrapper);
        return success();
    }

    @PostMapping(value = "/reCalculate")
    @ApiOperation("記賬咭內部審批-重新運算")
    public AjaxResult reCalculate(String appNo) throws Exception {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("APP_NO", appNo);
        DsgApplyBill bill = billService.getOne(wrapper);
        boolean success;
        if (DsgApplyBill.APP_TYPE_30 == bill.getAppType()) {
            success = payService.payAuditAll(bill);
        } else {
            boolean payAudit = payService.payAuditAll(bill);
            boolean carAudit = carService.carAuditAll(bill);
            boolean cusAudit = cusService.audit(bill);
            success = payAudit && carAudit && cusAudit;
        }
        if (success) {
            engine.recalculate(bill);
            return success();
        } else {
            return fail("內審失敗");
        }
    }
}
